﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LostLib
{
	/// <summary>
	/// Represents arguments for approves.
	/// </summary>
	/// <typeparam name="T">Subject type.</typeparam>
	[Serializable]
	public class ApproveEventArgs<T> : EventArgs
	{
		/// <summary>
		/// Creates new instance of ApproveEventArgs
		/// </summary>
		/// <param name="subject">Subject type.</param>
		public ApproveEventArgs(T subject) : base()
		{
			mSubject = subject;
		}

		private readonly T mSubject;
		/// <summary>
		/// Gets the object to be approved.
		/// </summary>
		public T Subject
		{
			get { return mSubject; }
		}

		private bool mApproved = false;
		/// <summary>
		/// Gets or sets whether object is approved.
		/// </summary>
		public bool Approved
		{
			get { return mApproved; }
			set { mApproved = value; }
		}
	}

	/// <summary>
	/// Extends ApproveEventHandler
	/// </summary>
	public static class ApproveHandlerExtensions
	{
		/// <summary>
		/// Performs approving returning its result.
		/// </summary>
		/// <typeparam name="T">Type of subject.</typeparam>
		/// <param name="handler">This handler.</param>
		/// <param name="sender">Sender of an event.</param>
		/// <param name="subject">Subject of approving.</param>
		/// <returns></returns>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
		public static bool Approve<T>(this EventHandler<ApproveEventArgs<T>> handler, object sender, T subject)
		{
			if (handler != null)
			{
				var args = new ApproveEventArgs<T>(subject);
				handler(sender, args);
				return args.Approved;
			} return true;
		}
	}
}
